package test.gen.core;

import gen.core.managers.SqlManager;
import gen.core.models.Sql;
import gen.core.utils.GenUtils;

import java.sql.Connection;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.lang.ArrayUtils;

public class SqlManagerTestCase extends TestCase {
	private Connection conn;
	
	public void setUp(){
		Properties aProperties = new Properties();
		aProperties.setProperty("driver", "org.sqlite.JDBC");
		aProperties.setProperty("url", "jdbc:sqlite:test.db");
		conn = GenUtils.createConnection(aProperties);		
	}
	
	public void tearDown() throws Exception{
		DbUtils.close(conn);
	}
	
	public void testList() throws Exception{
		SqlManager aSqlManager = new SqlManager();
		System.out.println(ArrayUtils.toString(aSqlManager.list(conn, "170fadf5-e6aa-4f0a-b436-d4526640a98d")));
	}
	
	public void testLoad() throws Exception{
		SqlManager aSqlManager = new SqlManager();
		Sql aSql = aSqlManager.load(conn, "170fadf5-e6aa-4f0a-b436-d4526640a98d");
		System.out.println(aSql);
	}
	
	public void testSave() throws Exception{
		SqlManager aSqlManager = new SqlManager();
		
		Sql aSql = new Sql();
		aSql.setId("170fadf5-e6aa-4f0a-b436-d4526640a98d");
		aSql.setTemplateId("477dcad3-484e-467a-9f88-887f5e461a04");
		aSql.setOwner("test");
		aSql.setCatagory("test");
		aSql.setContent("test11111");
		
		aSqlManager.save(conn, aSql);
		conn.commit();		
	}
	
	public void testDelete() throws Exception{
		SqlManager aSqlManager = new SqlManager();
		Sql aSql = new Sql();
		aSql.setTemplateId("477dcad3-484e-467a-9f88-887f5e461a04");
		aSql.setOwner("test");
		aSql.setCatagory("test");
		aSql.setContent("test11111");
		
		aSqlManager.save(conn, aSql);
		System.out.println("a sql was saved! it's id is "+aSql.getId());
		
		aSqlManager.delete(conn, aSql.getId());
	}
}
